<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>编辑文章 - CF-blog后台</title>
    <link rel="icon" type="image/x-icon" href="https://cdn.jsdelivr.net/gh/gdtool/zhaopp/cfblog/favicon.ico" />
	<link rel="shortcut icon" type="image/x-icon"  href="https://cdn.jsdelivr.net/gh/gdtool/zhaopp/cfblog/favicon.ico"/>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
    <!--//<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/editor.md@1.5.0/examples/css/style.css" />-->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/editor.md@1.5.0/css/editormd.css" />
    <link rel="stylesheet" href="https://cdn.staticfile.org/bootstrap-select/1.9.4/css/bootstrap-select.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/css/bootstrap-select.min.css">
    
    <script src="https://cdn.staticfile.org/jquery/2.2.4/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/js/bootstrap-select.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/js/i18n/defaults-zh_CN.min.js"></script>
	<style>
		.bootstrap-select>.dropdown-toggle {
		z-index:auto;
		}
	</style>
	
</head>
<body>

<nav class="navbar navbar-default" role="navigation">
	<div class="container-fluid">

	<div>
		<ul id="myTab" class="nav nav-tabs">
			<li class="active">
				<a href="/admin/" >返回文章</a>
			</li>
		</ul>
	</div>
	</div>
</nav>
<div id="myTabContent" class="tab-content" style="padding-top: 60px;">

		<div class="container">
			<h3>编辑文章</h3>   

			<form id="editForm" class="form-inline"  action="/" >

				<div class="form-group" style="width: 98%">
					<input type="hidden" class="form-control" name="id" id="id" readonly required="true">
					<input type="text" class="form-control" name="title" id="title" placeholder="标题" style="width: 800px;" required="true">
				</div>
				<div class="form-group">
					<label for="exampleInputEmail2">特色图片</label>
					<input type="url" class="form-control" style="width: 400px;"  name="img" id="img" placeholder="" >
				</div>
				<div class="form-group">
					<label for="exampleInputEmail2">永久链接</label>
					<input type="text" class="form-control" name="link" id="link" placeholder="" style="vertical-align:left !important;" required="true">
				</div>
				<div class="form-group">
					<label for="exampleInputEmail2">创建日期</label>
					<input type="datetime-local" class="form-control" id="createDate" name="createDate" placeholder="" style="vertical-align:left !important;" required="true">
				</div>
				<div class="form-group">
					<label for="exampleInputEmail2">分类</label>
					<select class="selectpicker" multiple name="category[]" id="category">
					</select>
				</div>
				<div class="form-group">
					<label for="exampleInputEmail2">标签</label>
					<input type="text" class="form-control" name="tags" id="tags" placeholder="标签1,标签2">
				</div>
				<div class="form-group">
					<label for="exampleInputEmail2">权重</label>
					<input type="text" class="form-control" name="priority" id="priority" value='0.5' placeholder="权重">
				</div>
				<div class="form-group">
					<label for="exampleInputEmail2">更新频率</label>
					    <select class="form-control" id="changefreq" name="changefreq">
						<option value="daily" >daily</option>
						<option value="hourly" >hourly</option>
						<option value="weekly" >weekly</option>
						<option value="monthly" >monthly</option>
						<option value="yearly" >yearly</option>
						<option value="never" >never</option>
						<option value="always" >always</option>
					    </select>
				</div>
					<a tabindex="0"  role="button"  type="submit" id="btn_saveEdit" class="btn btn-default" onclick="saveEdit()">保存</a>
					<a  type="submit" role="button" id="btn_delete" class="btn btn-warning" onclick="deleteArticle()">删除</a>
				<div id="content"><textarea style="display:none;"></textarea></div>
			</form>
		</div>

</div>

<script src="https://cdn.jsdelivr.net/npm/editor.md@1.5.0/editormd.js"></script>   
<script type="text/javascript">
	$(function() {
		$('#myTab li:eq(0) a').tab('show');
		//获取文章
		var articleJson = <!--{articleJson}-->;
		//获取分类
		var categoryJson = <!--{categoryJson}-->;

		
		var mdEditor = editormd("content", {
				//width  : "90%",
				height : 640,
				path   : "https://cdn.jsdelivr.net/npm/editor.md@1.5.0/lib/",
				appendMarkdown : articleJson.contentMD, //编辑器赋值
				saveHTMLToTextarea : true,
				mode : "markdown",
				tex  : true,
				tocm : true, 
				codeFold : true
			});
		//表单赋值
		$('#id').val(articleJson.id);
		$('#title').val(articleJson.title);
		$('#img').val(articleJson.img);
		$('#link').val(articleJson.link);
		$('#createDate').val(articleJson.createDate.replace(" ","T"));
		$('#tags').val(articleJson.tags);
		$('#priority').val( (articleJson.priority=== undefined ? "0.5":articleJson.priority) );
		$('#changefreq').selectpicker('val', (articleJson.changefreq === undefined ? "daily":articleJson.changefreq));
	
		$('#WidgetCategory').val(JSON.stringify(categoryJson));
		var category = $('#category');
		category.empty();
		for (var i = 0; i < categoryJson.length; i++) {
			category.append('<option id=' + categoryJson[i] + ' value=' + categoryJson[i] + '>' + categoryJson[i] + '</option>');
		}
		category.selectpicker('val',articleJson.category);
	});
    //保存按钮(编辑)
    function saveEdit(){
        $.ajax({
            type: "POST",
            dataType: "json",
			contentType: "application/json; charset=utf-8",
            url: "/admin/saveEdit/" ,//url
            data: JSON.stringify($("#editForm").serializeArray()), 
            success: function (result) {
                alert(result.msg);
            }
        });
    }
    //删除
    function deleteArticle(){
        if (confirm("确定删除吗?删除后重新发布才能生效")==false){ 
            return false; 
          }
        $.ajax({
            type: "POST",
            dataType: "json",
			contentType: "application/json; charset=utf-8",
            url: "/admin/delete/"+$('#id').val() ,//url
            data: [{"id":$('#id').val()}],
            success: function (result) {
                alert(result.msg);
            }
        });
    }
</script>
  
</body>

</html>
